//爬虫控制器
import { Controller, Param, Get, Post } from "routing-controllers";
import { AppConfigContainer } from '../../lib/utils/app.config';
import  Format from 'string-format'
let Crawler = require('crawler');
import logger from '../../lib/utils/logger';

@Controller('/spider')
export class SpiderController {


    @Post('/lottery/ssq')
    async  lottery_ssq() {
        return new Promise((reslove, reject) => {
            let c = new Crawler({
                maxConnections: 10,
                jQuery:true,
                // This will be called for each crawled page
                callback: function (error: any, res: any, done: any) {
                    try {
                        if (error) {
                            console.log(error);
                            reject({ success: false })
                        } else {
                            var $ = res.$;
                            // $ is Cheerio by default
                            //a lean implementation of core jQuery designed specifically for the server

                            let tr = $('#tdata tr');
                            logger.info('###################开始获取结果###########################');
                            
                            
                            tr.each((i: number, m: any) => {
                                //console.log(i)
                                let td = $(this).find('td');
                                td.each((idx:number,tde:any)=>{
                                    logger.info('-->' + $(tde).text());
                                });
                            });
                            reslove({ success: true })

                        }
                    } catch (e) {
                        logger.error(e);
                        reject({ success: false })
                    }

                    done();
                }
            });
            let url = Format(AppConfigContainer.get('lottery_ssq'), '50')

            c.queue(url);
        });


    }

}